package test11;

import java.io.*;

public class Test11 {
    /**
     * 自定义一个对象，并对该对象进行序列化与反序列化。
     */

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        System.out.println("============第11题=============================");
        test11();
    }

    public  static void  test11() throws IOException, ClassNotFoundException {
        Person person1 = new Person();
        person1.setName("eternal");
        person1.setAge(80);
        System.out.println(person1);

        // 序列化对象到文件中
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("template"));
        oos.writeObject(person1);
        oos.close();

        // 反序列化
        File file = new File("template");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        Person newPerson = (Person) ois.readObject();
        ois.close();
        System.out.println(newPerson.toString());
    }
}


